package com.easepal7506a.project.ui.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.easepal7506a.project.Constant.CommmandNum;
import com.ogawa.base.Constant.Constants;
import com.easepal7506a.project.R;
import com.easepal7506a.project.network.CommondManager;
import com.easepal7506a.project.view.loopview.LoopView;
import com.easepal7506a.project.view.loopview.OnItemSelectedListener;
import com.example.reslib.utils.DisplayUtil;
import com.example.reslib.utils.ToastUtils;
import com.ogawa.base.callback.NormalCallback;
import com.ogawa.base.network.BaseResponse;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;

/**
 * Created by FHB on 2018/3/28.
 */

public abstract class BaseTimeTaskFragment extends Fragment {
    private String mTime= Constants.TIME_15;
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }



    public  void sendCommond(String code){
        CommondManager.getInst().sendShortCommond(code, new NormalCallback() {
            @Override
            public void onSuccess(BaseResponse<String> program) {

            }

            @Override
            public void onFailure(String error) {
                ToastUtils.showLongToast(getContext(),error+"");

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }


    public void showDialog() {

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
        LayoutInflater inflater =this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_loopview_7506, null);
        dialogBuilder.setView(dialogView);
        TextView mTvCancle = dialogView.findViewById(R.id.dialog_cancle);
        TextView mTvOk = dialogView.findViewById(R.id.dialog_ok);

        LoopView loopView = (LoopView) dialogView.findViewById(R.id.loopView_7506);
        final ArrayList<String> list = new ArrayList<>();
        list.add(Constants.TIME_5);
        list.add(Constants.TIME_10);
        list.add(Constants.TIME_15);
        list.add(Constants.TIME_20);
        list.add(Constants.TIME_25);
        list.add(Constants.TIME_30);
        //  滚动监听
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                mTime = list.get(index);

            }
        });

        // 设置原始数据
        loopView.setItems(list);
        loopView.setCurrentPosition(2);
        loopView.setItemsVisibleCount(5);
        loopView.setDividerColor(getResources().getColor(R.color.white));
        loopView.setOuterTextColor(Color.GRAY);
        loopView.setCenterTextColor(Color.CYAN);
        final AlertDialog alertDialog = dialogBuilder.create();
        //加上下面两句才能显示圆角 不然圆角处会有白色效果
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alertDialog.show();
        mTvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        mTvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(mTime)){
                    getTime(mTime);
                    alertDialog.cancel();
                }
            }
        });
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int)(DisplayUtil.getScreenWidthPx(getContext())*0.9); //设置宽度
        window.setAttributes(lp);


    }



    public void sendTime(String time){
        if(time.equals(Constants.TIME_5)){
            sendCommond(CommmandNum.TIME_5);

        }
        else if(time.equals(Constants.TIME_10)){
            sendCommond(CommmandNum.TIME_10);
        }
        else if(time.equals(Constants.TIME_15)){
            sendCommond(CommmandNum.TIME_15);
        }
        else if(time.equals(Constants.TIME_20)){
            sendCommond(CommmandNum.TIME_20);
        }
        else if(time.equals(Constants.TIME_25)){
            sendCommond(CommmandNum.TIME_25);
        }
        else if(time.equals(Constants.TIME_30)){
            sendCommond(CommmandNum.TIME_30);
        }

    }

    public abstract void getTime(String time);


}
